# 错误评估文件（之后再改）
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import roc_curve, accuracy_score, confusion_matrix, auc
import matplotlib.pyplot as plt

# 加载数据集

data = load_breast_cancer()

X = data.data

y = data.target

# 划分训练集和测试集

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 使用决策树构建个体学习器

clf = DecisionTreeClassifier()

clf.fit(X_train, y_train)

# 使用AdaBoost利用决策树构建集体学习器

ada = AdaBoostClassifier(base_estimator=clf, n_estimators=100, random_state=42)

ada.fit(X_train, y_train)
print("X_train shape is: ", X_train.shape)
print("X_test shape is: ", X_test.shape)
print("X_test is: ", X_test)

# 预测测试集结果

y_pred = ada.predict(X_test)

# 计算ROC曲线

fpr, tpr, thresholds = roc_curve(y_test, y_pred)

roc_auc = auc(fpr, tpr)

# 输出ROC曲线图

plt.figure()

plt.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)

plt.plot([0, 1], [0, 1], 'k--')

plt.xlim([0.0, 1.0])

plt.ylim([0.0, 1.05])

plt.xlabel('False Positive Rate')

plt.ylabel('True Positive Rate')

plt.title('Receiver Operating Characteristic')

plt.legend(loc="lower right")

plt.show()

# 计算准确率

accuracy = accuracy_score(y_test, y_pred)

print('Accuracy: {:.2f}%'.format(accuracy * 100))

# 计算混淆矩阵

cm = confusion_matrix(y_test, y_pred)

print('Confusion Matrix:')

print(cm)